package ldh.im.base.service;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import ldh.common.PageResult;
import ldh.im.base.dao.ImMessageDao;
import ldh.im.base.pojo.ImMessage;
import ldh.im.base.pojo.where.ImMessageWhere;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author: ldh
 * @date: 2024-03-21 10:36:17
*/
@Service("imMessageService")
public class ImMessageService {

	@Resource
	private ImMessageDao imMessageDao;
	
	@Transactional
	public ImMessage insert(ImMessage imMessage) {
		int n = imMessageDao.insert(imMessage);
		if (n != 1) {
			throw new RuntimeException("ImMessage insert error!");
		}
		return imMessage;
	}
	
	@Transactional
	public ImMessage insertSelective(ImMessage imMessage) {
		int n = imMessageDao.insert(imMessage);
		if (n != 1) {
			throw new RuntimeException("ImMessage insertSelective error!");
		}
		return imMessage;
	}

	@Transactional
	public ImMessage updateById(ImMessage imMessage) {
		int  n = imMessageDao.updateById(imMessage);
		if (n != 1) {
			throw new RuntimeException("ImMessage update error!");
		}
        return imMessage;
     }

	@Transactional
	public ImMessage updateNotNullById(ImMessage imMessage) {
		int  n = imMessageDao.updateNotNullById(imMessage);
		if (n != 1) {
			throw new RuntimeException("ImMessage update error!");
		}
        return imMessage;
     }


    @Transactional
    public void deleteById(Long id) {
		int n = imMessageDao.deleteById(id);
		if (n != 1) {
			throw new RuntimeException("ImMessage delete error!");
		}
    }

	@Transactional(readOnly = true)
	public ImMessage getById(Long id) {
		ImMessage imMessage = imMessageDao.getById(id);
		return imMessage;
    }
    
	@Transactional(readOnly = true)
	public ImMessage getByFromUserIdAndClientId(String fromUserId, String clientId) {
		ImMessage imMessage = imMessageDao.getByFromUserIdAndClientId(fromUserId, clientId);
		return imMessage;
    }
    
	
	@Transactional(readOnly = true)
	public PageResult<ImMessage> findByImMessageWhere(ImMessageWhere imMessageWhere) {
		long total = imMessageDao.findTotalByImMessageWhere(imMessageWhere);
		List<ImMessage> imageList = new ArrayList<ImMessage>();
		if (total > 0) {
			imageList  = imMessageDao.findByImMessageWhere(imMessageWhere);
		}
		
		return new PageResult<ImMessage>(imMessageWhere, total, imageList);
	}
	
	
}
